﻿using System;
using Koonsoft.HYCRM.BLL;
using Koonsoft.HYCRM.DAL;
using Koonsoft.HYCRM.Model;

public class ObjectCacheHelper
{
    /// <summary>
    /// 获取一个对象的缓存
    /// </summary>
    /// <param name="objectId"></param>
    /// <returns></returns>
    public static T GetObjectCache<T>(string objectId)
    {
        ObjectCache objCache = ObjectCacheDAL.GetObjectCache(objectId);
        if (objCache == null) return default(T);
        string xml = objCache.ObjectData;
        T entity = (T)XmlObjectHelper.XMLToObject(xml, typeof(T));
        return entity;
    }

    /// <summary>
    ///插入一条记录
    /// </summary>
    /// <param name="entity"></param>
    /// <param name="objectId">记录ID,在外面生成</param>
    public static void AddObjectCache(Object entity, string objectId)
    {
        string xml = XmlObjectHelper.ObjectToXML(entity);
        var objectCache = new ObjectCache
                              {
                                  ObjectData = xml,
                                  OperateType = 0,
                                  ObjectID = objectId
                              };
        ObjectCacheDAL.AddObjectCache(objectCache);
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="objectId"></param>
    public static void DeleteObjectCache(string objectId)
    {

    }

    /// <summary>
    /// 更新缓存对象
    /// </summary>
    /// <param name="entity"></param>
    /// <param name="objectID"></param>
    public static void UpdateObjectCache(Object entity, string objectID)
    {
        string xml = XmlObjectHelper.ObjectToXML(entity);
        var objectCache = new ObjectCache
        {
            ObjectData = xml,
            OperateType = 1,
            ObjectID = objectID
        };

        ObjectCacheDAL.UpdateObjectCache(objectCache);
    }
}